package com.example.myapplication.ViewPageFragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class ViewPager2Adapter extends RecyclerView.Adapter<ViewPager2Adapter.ViewPageViewHolder> {

    private List<PageItem> pageItems;

    public ViewPager2Adapter(List<PageItem> pageItems) {
        this.pageItems = pageItems;
    }

    // 创建 ViewHolder
    @NonNull
    @Override
    public ViewPageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewPageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page,parent,false));
    }

    // 绑定 ViewHolder 的数据
    @Override
    public void onBindViewHolder(@NonNull ViewPageViewHolder holder, int position) {
        PageItem item = pageItems.get(position);
        holder.mtv.setText(item.title); // 设置标题
        holder.mContainer.setBackgroundResource(item.color); // 设置背景色
    }

    // 获取列表长度
    @Override
    public int getItemCount() {
        return pageItems.size();
    }

    // ViewHolder 类
    class ViewPageViewHolder extends RecyclerView.ViewHolder{

        TextView mtv; // 标题 TextView
        RelativeLayout mContainer; // 根容器 RelativeLayout

        public ViewPageViewHolder(@NonNull View itemView) {
            super(itemView);
            mContainer = itemView.findViewById(R.id.container); // 获取根容器
            mtv = itemView.findViewById(R.id.tvTitle); // 获取标题 TextView
        }
    }

    public static class PageItem {
        public String title;
        public int color;

        public PageItem(String title, int color) {
            this.title = title;
            this.color = color;
        }
    }
}


